Thanks your answer.
My problem is that I want another sensors attribute to enter this sensor (alarm_ready).
What I try to achive is a sensor, that is published by a mqtt broker, shall change its attrubute even if the state itself have not changed. The reason is that this sensor changes occurs very seldom and I want to trigger an update of the sensor for the receiver of the mqtt messages, Right now the sensor in the receiving side is almost always “not availible” due to the slow update frequence. The “last update” attribute that I want to copy into the “alarm_ready” sensor updates everytime that all data is updated from the alarm system
You are. You may be able to make an automation and force an update. Other than that, you’ll need to get inventive. As far as I know, MQTT sensors are the only sensors that allow you to add attributes as a template.
#pass entity_id as argument from call
sensor = data.get('entity_id')
#read old state
oldstate = hass.states.get(sensor)
#write old state to entity and force update to record database
hass.states.set(sensor, oldstate.state , oldstate.attributes, force_update=True)
what kind of sensor is your sensor.alarm_active?
for some sensors (like MQTT sensor) you can use force_update configuration variable to achieve the same result without a script.
I have figured out so far that the MQTT sensor is defined by the recieved state_topic, but I have not figured out how to define and send these state_topics on the broker/sender side.
Hi @a94marbo : thank you very much for this solution. It is working great! For everyone else: if you need a forced sensor update to make nice graphics, this is your solution. It will create new data points at a fixed interval, even if the sensor value is the same.
This is a very old topic (> 2 years) and there have been over two dozen new versions of Home Assistant released since May 2019. The neatest, simplest way to create a Template Sensor that is updated on a periodic basis is to create a Trigger-based Template Sensor with a Time Pattern Trigger. The documentation’s first example uses a Time Pattern Trigger:
# Example configuration entry
template:
- trigger:
- platform: time_pattern
# This will update every night
hours: 0
minutes: 0
sensor:
# Keep track how many days have past since a date
- name: Not smoking
state: '{{ ( ( as_timestamp(now()) - as_timestamp(strptime("06.07.2018", "%d.%m.%Y")) ) / 86400 ) | round }}'
unit_of_measurement: "Days"
Thanks for this insight @123 . I’m not fully understanding this yet, could you please help me out a bit?
The situation:
i have a sensor named sensor.xxx
sensor.xxx is only updating once every few hours (whenever a new value comes in)
my Grafana graphs show “no data” when i use an interval of 1 hour (because the last update was 3 hours ago)
i’d like sensor.xxx to update its value every minute, even if the value itself has not changed. In this way, Grafana will not show “no data” when using an interval of 1 hour
The python script works perfectly for that. How can i do the same with a Trigger-based template sensor with a time pattern trigger?
No good deed goes unpunished. What I posted was meant to provide insight into new ways to solve old problems … not necessarily this old problem. Trigger-based Template Sensors are useful for applications where the template computes the value.
In this situation, nothing is computed; the goal is to force an existing entity to refresh its value. Typically, homeassistant.update_entity is sufficient (there’s an example posted way up in the thread) but there are use-cases where it’s insufficient because Home Assistant tries to avoid recording entity values that don’t change (and so we arrive at the python_script to force it).
Anyway, if anyone believes what I posted above isn’t useful to the discussion, let me know and I’ll delete it. I don’t want to lead anyone down the wrong path.
I am looking for a solutions like this for a while now, and i cant find the solution. I have a few sensors through a few ESPs. Mainly temperature and humidity. These values dont update that often but i would like to fill the database even if the sensor is unchanged. Exactly what you are doing.
I’m very new with HA and i’m not very knowledgable with coding. Do you have an example for me? However i search i don’t seem to find an answer anywhere.
The second line of this python script can be made dynamic as well if you want to (by using ‘entity_id’)
edit: please note that this solution will only work for sensors that go back to zero (which is the case for water and gas consumption, but most likely not for temperature and humidity sensors)