How to read-modify-write value of a setting in esphome? - esphome

For example, to increase/decrease a thermostat temperature using 2 buttons, I have to read the current value, add (or subtract) 0.1°, then update the setting.
The question is how to do it with and (if possible) without Esphome's lambdas.

I only managed to do it this way:
on_press:
- lambda: !lambda |-
id(_thermostat).target_temperature += .5;
ESP_LOGI("main", "target_temperature: %f", id(_thermostat).target_temperature);

Related

Anylogic: Dynamically change source rate using variable/slider

I am trying to dynamically change the source Arrival rate using a variable "arrivalRate" linked to a slider (see image).
However, during the simulation the initial rate remains the same, even when I change the arrivalRate. I know that the arrivalRate variable is changing successfully (it is an int) - but this has no effect on the source rate during the simulation.
Anyone have an idea what the issue is - or how to fix it?
Whenever you see the = sign before a field, it means it's not dynamic, it is only evaluated at the start of the model or at the element creation and will not change throughout the simulation run unless you force it. In other words, the variable arrivalRate is checked only once to assign the source's arrival rate and that's it.
Now if you want to change it dynamically, in the slider's Action field, write the following:
source.set_rate( arrivalRate );

Changing amount of resource sets in Seize-block programmatically

So the problem is:
How can I change the amount in "Resource Sets" programmatically? I'am planning on changing this value back and forth from 1 and 2 given some conditions during the simulation run.
Amount I want to change is marked in yellow below.
Emile answer is right, but if you insist in using what you are using, you can click on the equal sign and make it a function:
and in the function (which i made take a random number between 1 and 2, but you can do what you want), you use the following (the function returns a type ResourcePool[][])
ResourcePool[][] rp = new ResourcePool[1][num];
for(int i=0;i<num;i++){
rp[0][i]=resourcePool;
}
return rp;
In your case, it seems you are using one resource alternative only.
Why don't you choose units of the same pool, then the number of units to be seized becomes dynamic.

Setting up openai gym

I've been given a task to set up an openai toy gym which can only be solved by an agent with memory. I've been given an example with two doors, and at time t = 0 I'm shown either 1 or -1. At t = 1 I can move to correct door and open it.
Does anyone know how I would go about starting out? I want to show that a2c or ppo can solve this using an lstm policy. How do I go about setting up environment, etc?
To create a new environment in gym format, it should have the 5 functions mentioned in the gym.core file.
https://github.com/openai/gym/blob/e689f93a425d97489e590bba0a7d4518de0dcc03/gym/core.py#L11-L35
To lay this down in steps-
Define observation space and action space for your environment, preferably using gym.spaces module.
Write down the step function which performs agent's action and returns a 4 tuple containing - next set of observations from the environment , reward ,
done - a boolean indicating whether the episode is over , and some extra info if you want.
Write a reset function for the environment to reinitialise the episode to a random start state and return a 4 tuple similar to step.
These functions are enough to be able to run an RL agent on your environment.
You can skip the render, seed and close functions if you want.
For the task you have defined,you can model the observation and action space using Discrete(2). 0 for first door and 1 for second door.
Reset would return in it's observation which door has the reward.
Then agent would choose either of the door - 0 or 1.
Then perform a environment step by calling step(action), which will return agent's reward and done flag as true - signifying that the episode is over.
Frankly, the problem you describe seems too simple to accomplish for any reinforcement learning algorithm, but I assume you have provided that as an example.
Remembering for longer horizons is usually harder.
You can read their documentation and toy environments to understand how to create one.

How to change properties of point tracker without re-initializing tracking?

I am working on point tracker, I know I can set the properties of tracker initilay by using :
tracker = vision.PointTracker('NumPyramidLevels',3,'MaxBidirectionalError', 2, 'MaxIterations',30,'BlockSize',[5 5]);
initialize(tracker, points.Location, objectFrame);
and then by using step method the tracking is working fine.
my question how to make these properties dynamic during the tracker is working, I used same method inside the while statment as following:
while ~isDone(videoFileReader)
frame = step(videoFileReader);
[points, validity,scores] = step(tracker, frame);
tracker = vision.PointTracker('NumPyramidLevels',3,'MaxBidirectionalError', 2, 'MaxIterations',30,'BlockSize',[5 5]);
initialize(tracker, points, objectFrame);
end
but the problem here that the tracker is initialized again! but I need to continue on the prevouis fram state without initialize the tracking again.
Unfortunately, the short answer is "no".
vision.PointTracker is a System Object, which is a special kind of MATLAB class. In a System Object, properties may be tunable or non-tunable. Non-tunable properties can only be changed before you call the step method of the object for the first time. Currently, all properties of vision.PointTracker are non-tunable, which means you cannot change their values inside the loop.

How to deselect cells in uitable / how to disable cell selection highlighting?

I created the following uitable:
actually every single row is an indpendent uitable, so the figure shown contains 5 uitables besides the header. Why I'm doing that was the issue of my last question, resulting in the shown table. Fully executable code you can find in the answer here (or a minimal example below). Solutions using a general GUI are also there, but it would blow up the code too much, and actually it just seems to be a kind of bug.
It can be seen that everytime I jump to the next row, therefore to another uitable, the last selection remains highlighted, which looks stupid, though it doesn't matter for the functionality.
There is the 'SelectionHighlight' property for uitables, sounds like a solution, but it is not changing anything. I used it as following:
set(src,'SelectionHighlight','off') %where src is the handle of the current uitable
at various places: at the end of a 'CellSelectionCallback', at the end of a 'CellEditCallback' and as global property. But everytime the last cell remains selected. Actually I don't need selection at all.
How can I disable the whole selection or selection highlighting property for all my uitables?
How do I have to use this property, that it has an effect?
Alternatively: how can I change the "highlighting" color (and therefore text-color) so the highlighting is just not visible anymore?
Apparently this issue appears also in other contexts.
I created a minimum executable example, where one can select a number between 1 and 3 in every row.
function minimalTable
%basic properties
line_height = 21.32;
table_height = 3*line_height;
lh = line_height/table_height;
h = figure('Position',[200 100 202 table_height],'numbertitle','off','MenuBar','none');
% addrow(figurehandle,number of row, percentage lineheight)
% every function call creates a new row, later dynamically
addRow(h,1,lh);
addRow(h,2,lh);
addRow(h,3,lh);
end
function modifySelection(src,~)
set(src,'SelectionHighlight','off')
waitfor(src)
end
function [th] = addRow(fh,k,lhp)
selector = { '1'; '2' ; '3' };
defaultData = {'select number...'};
columnformat = { {selector{:}} };
columneditable = true;
th = uitable(fh,'Units','normalized','Position',[0 1-k*lhp 1 lhp],...
'Data', defaultData,...
'ColumnName', [],...
'ColumnWidth', {200},...
'ColumnEditable', columneditable,...
'ColumnFormat', columnformat,...
'RowName',[],...
'SelectionHighlight','off',...
'CellEditCallback',#modifySelection);
end
results in:
After some deeper research I found out, that the Matlab Support comes out with the following solution:
%overwrite data with a dummy and restore the old data afterwards, to force deselection
function modifySelection(src,~)
...
temp = get(src,'Data')
set(src,'Data',{ 'dummy' });
set(src,'Data', temp );
end
Doing this the blue highlighting is gone, BUT the dotted line around the last selected cell remains!
But I found a solution resolving this, which also makes the first part dispensable.
function modifySelection(src,evt)
...
fh = get(src,'parent'); % get parent figure handle
copyobj(src,fh); % copy uitable to parent figure
delete(src); % delete current uitable
end
Which results in the desired behaviour:
Drawback of the second solution: it lags a little (probably just on slow machines), because of the creation of a new object.
Allright, I found a solution for deselecting cells:
First of all, this requires some Java. But dont worry, it will still look like Matlab :)
1. You will need the script findjobj by Yair Altman: TMW File-Exchange: findjobj
2. You need the handle of your table, lets call it mtable. Then you need the underlying Java-table and do some stuff to get the right objects and set some properties. You can do this by:
jscroll=findjobj(mtable);
h=jscroll.getComponents;
viewport=h(1);
a=viewport.getComponents;
jtable=a(1); %com.mathworks.hg.peer.ui.UITablePeer
jtable.setRowSelectionAllowed(0);
jtable.setColumnSelectionAllowed(0);
3. Now the more tricky part (at least it was for me): If you have some Callback for CellSelectionChanged, but you dont want to excecute this now, you have to turn it off temporary:
set(mtable, 'CellSelectionCallback', []);
Now you can change the selection by:
jtable.changeSelection(row-1,col-1, false, false);
%Java-> zero ^= one <-Matlab
And now, I was expecting, when setting the CellSelectionCallback back to its original function, everything would be fine. Nope, it was excecuting the Callback. I still dont know the exact reason, but it seems to me, that calling jtable.changeSelection() the selection changes and then is calling the specified Callback, but the caller function is not waiting while this process is running. So what I tried (and I dont know if this is the best way to do it, but it is working very well) is to just pause for a second and then set the Callabck back:
pause(1)
set(mtable, 'CellSelectionCallback', #myOriginalFunction);
4. Now just one more thing: My purpose was just to change the selection to some other cell. Yours is to deselect. I dont know anything about the Java components, but I succeeded by just setting the row/column parameter to -1:
jtable.changeSelection(-1,-1, false, false);
Finally I managed to solve this problem by using many things explained on undocumentedmatlab.com and other posts. I am not sure if all the lines are necessary to call. Note, that this will only be available for the documented Matlab-uitable which appears first in Version 2008 (a or b, I'm not sure about that).
EDIT there are a lot of other functions/parameters etc. you can use, that are undocumented. Just to see what is possible, you can take a look with the autocomplete. Just use it on the jtable. and Tab will display them. For a documentation on those elements you should probably search for a Java-doc.
Just a small "dynamic" minimal example (wait 3 seconds to see a change ;-) ):
function startUitable()
xDat=ones(5,3);
h=figure('Tag','TestFigure');
mtable=uitable('Tag','TestUITABLE');
rowField=uicontrol('units','normalized','Style','edit','Position',[0.4 0.9 0.1 0.1],'parent',h,'Tag','rowField');
colField=uicontrol('units','normalized','Style','edit','Position',[0.6 0.9 0.1 0.1],'parent',h,'Tag','colField');
set(mtable, 'Units','normalized','Position',...
[0.01 0.01 0.8 0.8], 'Data', xDat,...
'ColumnEditable', [false, false,false],...
'ColumnWidth', 'auto')
myButton=uicontrol('units','normalized','Style','pushbutton','parent',h,'Position',[0.04 0.9 0.3 0.1],'String','change Selection')
set(myButton,'Callback',#changeSelection)
end
function changeSelection(~,~,~)
mtable=findobj('Tag','TestUITABLE');
jscroll=findjobj(mtable);
h=jscroll.getComponents;
viewport=h(1);
a=viewport.getComponents;
jtable=a(1); %com.mathworks.hg.peer.ui.UITablePeer
% jtable.setRowSelectionAllowed(0);
% jtable.setColumnSelectionAllowed(0);
row=str2num(get(findobj('Tag','rowField'),'String'));
col=str2num(get(findobj('Tag','colField'),'String'));
jtable.changeSelection(row-1,col-1, false, false);
end