I like to make a sensor with no value that i can use the hass.states.set from a script to set values. What kind of sensor can i use ?
The states in the state machine do not have to correspond to real entities. You can make up entity_idâs, as long as they have a domain and object id (i.e., domain.objec_id
.) See here, especially this statement:
You can make up any combination of domain and object id, even overwriting existing states.
Thank you
Hi Thomas,
Can you send through an example of how you set the dummy sensor value vis a script please?
Here is a script Iâve used (python_scripts/set_state.py
):
if 'entity_id' not in data:
logger.warning("===== entity_id is required if you want to set something.")
else:
data = data.copy()
inputEntity = data.pop('entity_id')
inputStateObject = hass.states.get(inputEntity)
if inputStateObject:
inputState = inputStateObject.state
inputAttributesObject = inputStateObject.attributes.copy()
else:
inputState = 'unknown'
inputAttributesObject = {}
if 'state' in data:
inputState = data.pop('state')
logger.debug("===== new attrs: {}".format(data))
inputAttributesObject.update(data)
hass.states.set(inputEntity, inputState, inputAttributesObject)
You call it like this:
- service: python_script.set_state
data:
entity_id: sensor.blah
state: blah blah
abc: 123
xyz: do re mi
This will result in an entity with an id of âsensor.blahâ whose state will be âblah blahâ and which will have two attributes â abc = 123, and xyz = âdo re miâ.
You need to specify at least entity_id. If you specify state, it will set the state to that value. If you donât specify state, then the state will not change (or be set to âunknownâ if the entity did not yet exist in the state machine.) All other parameters will be written as attributes.
Big thanks for that,
(Iâm only noting this for others)
One part thatâs missing in your instruction was to add in configuration.yaml:
python_script:
Maybe you could explain why set_state.py needs to sit in a dir. called python_scripts/ not python_script/ but all code refers to âpython_scriptâ??
Thanks again,
Rob.
Sorry, youâre right, I forgot to mention that. It is documented here.
Um, because you might have more than one? Itâs just the way someone wrote it I guess.
Can you please help me out why the below config does not work?
It does not pass the data in the attributes for state and data.
- service: python_script.set_state
data:
entity_id: sensor.water
state: "{{ states.input_text.energyid_water.state }}"
date: "{{ states.sensor.date_time_iso.state }}"
Two very basic problems. 1, the indentation is wrong. 2. youâre using data instead of data_template.
Thanks stupid mistake.
Question, this sensor.water does not survive a hass reboot.
All sensor data is gone, I want to store the last meter value in that sensor.
Well then you probably need to use an input_text and maybe an input_number and save the values with an automation.
Sorry to resurrect this but I just came across it and wondered how if at all this is different from this:
which suggests this, also 18 lines but different and which I have used for some time to set the state of an existing sensor.
inputEntity = data.get('entity_id')
if inputEntity is None:
logger.warning("===== entity_id is required if you want to set something.")
else:
inputStateObject = hass.states.get(inputEntity)
inputState = inputStateObject.state
inputAttributesObject = inputStateObject.attributes.copy()
for item in data:
newAttribute = data.get(item)
logger.debug("===== item = {0}; value = {1}".format(item,newAttribute))
if item == 'entity_id':
continue # already handled
elif item == 'state':
inputState = newAttribute
else:
inputAttributesObject[item] = newAttribute
hass.states.set(inputEntity, inputState, inputAttributesObject)
Im trying to do something similar. I created and input_number and i want this value that it is set to (1-25) to feed back into a card
Using list-card and want to be able to set the max amount of rows based on the slider. The list-card config is looking for row_limit: â10â or replace 10 with any number up to 25.
I tried replacing the â10â with things like {{ state(âinput_number.rss_sliderâ) }} but no such luck. How can i retrieve the state value and use it?
Thanks
edit:I realize this can be done with a conditional card but its 25x the amount of code to handle it.
Sorry, not following you. An input_number
is a real entity. You change it and use its value like any normal real entity. You do not need, nor should use use, that script to change it.
i must have not done a good job explaining myself. Not trying to change the state of it but rather use the state(the number its set to) as a variable in a card
Then Iâm confused. Why canât you just display the input_number
entity like you would display any entity in the UI? Iâm also confused because you asked on this topic which was about creating a âdummyâ entity, which is not the case here. Sorry, but I think maybe youâre asking in the wrong place, or Iâm just misunderstanding.
sorry i didnt look at the header. i read some responses here that were similar to what i was looking for