Hi! I’m new in HA and some aspects are not oblivious for me. Also reading of documentation does not help me in this particular case. I have some sensors, that sends timestamp values over mqtt. It works fine, but timezone of timestamp is wrong (it’s not UTC and not my current timezone, it’s shifted to 3 hours to the future) and I can’t change it on the sensor side. So I need to reduce value to 10800.
Without value_template it shows me timestamp as integer in states.
I tried few different variants of value templates.
This one works fine and in states tab I can also see timestamp as integer value:
value_template: "{{ value }}"
But this one returns me 0 (zero):
value_template: "{{ value | int }}"
I tried to test first variant in template tab and find out that Home Assistant converted timestamp value to it’s own object.
Template:
{% set val = states.sensor.corr_pir_ts %}
{{ val }}
Result:
<template state sensor.corr_pir_ts=0; friendly_name=corr_pir_ts @ 2020-05-31T17:35:04.389712+05:00>
Please, tell me somebody how can I use integer value from this sensor? For example I want to make custom sensor with “platform: template” and use value of states.sensor.corr_pir_ts inside it’s template. I spend three days for it, but without any success.
If you need to reduce the received timestamp value by 10800 seconds you can use value_template to do that.
- platform: mqtt
state_topic: "/sapfir/823474/pir-time"
name: "corr_pir_ts"
value_template: "{{ value | int - 10800 }}"
If the result of the template is zero (or a negative number) that’s because the int filter was unable to convert the received data into an integer value.
There seems to be some sort of miscommunication or misunderstanding that is causing confusion.
If the payload published to this topic:
/sapfir/823474/pir-time
is a timestamp which is simply a numeric value that looks something like this:
1590942969
then this MQTT Sensor configuration will definitely work correctly and not result in 0 or unknown:
- platform: mqtt
state_topic: "/sapfir/823474/pir-time"
name: "corr_pir_ts"
value_template: "{{ value | int - 10800 }}"
If that configuration is reporting 0 or unknown then the received payload is not a simple numeric value.
Please post an example of the payload published to /sapfir/823474/pir-time. You can acquire the payload by using Developer Tools > MQTT > Listen to a topic (and have the source of the timestamp publish it):
Yep, thank you very much! It seems it works, but there are another problem - converting modified timestamp back to datetime. So I have a problem with at least one of converting processes after modification of timestamp depends on where I tried to reduce the value. I’ve made few sensors to check different varians of templates. There are templates and results below:
value_template: "{{ value | int - 10800 }}" -> 1590950431 # yes, this is correct timestamp with timeshifting
value_template: "{{ value | int - 10800 | timestamp_local }}" -> unknown
value_template: "{{ as_timestamp(value | int - 10800) }}" -> None
value_template: "{{ strptime((value | int - 10800),'%Y-%m-%d %H:%M:%S')) }}" -> None
So, I left first variant as a base and made second template sensor with template:
{% set ts = states.sensor.corr_pir_ts.state | int %}
{{ ts | timestamp_local }}
And it’s state is “1970-01-01 05:00:00” while in template tab it works perfectly and shows correct date (2020-05-31 23:43:43).
If it works now, why didn’t it work before? When I originally proposed this would work:
value_template: "{{ value | int - 10800 }}"
You replied:
Clearly something must have changed.
In addition, the post you marked as Solution, isn’t the solution to the original question. There was never any “strange autoconversion”, you simply misunderstood an entity’s state property and how to reference it correctly.