[Solved] Strange autoconversion of mqtt values

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.

Sensor description:

- platform: mqtt
  state_topic: "/sapfir/823474/pir-time"
  name: "corr_pir_ts"

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.

Thank you.

For all entities, the state property’s value is always a string. From State Object:

Field Description
state.state String representation of the current state of the entity. Example off.

When looking at the values in the State column, in Developer Tools > States, they are all strings (even if they appear to be numeric).

UPD: I feel myself as an idiot. Solving is to get “state from state” and template should be like this:

{% set val = states.sensor.corr_pir_ts.state %}
{{ val }}

Thank you! :slight_smile:

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.

No, I can’t it. Below there are the templates and results:

{{ value | int }} -> 0
{{ value | int - 10800 }} -> unknown

Cause HA converts timestamp value to the object. At the moment I have another error. Second sensor I’ve made has this template:

        {% set ts = (states.sensor.corr_pir_ts.state | int) - 10800 %}
        {{ ts | timestamp_local }}

This template woks as expected in templates checking tab. But when I’m trying to use it for sensor result is “1970-01-01 02:00:00”. I don’t know, why.

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):

To prove it works, I created an MQTT Sensor (using the configuration I posted above) and it initially appears like this:

Then I used MQTT Explorer to publish 11800 to /sapfir/823474/pir-time and the sensor’s state changed to 11800 - 10800 = 1000


Because the sensor’s value is 0 and timestamp_local converts it to your local time, which I guess is UTC+2.

I’m UTC-5 so a timestamp of 0 gets converted like this:

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).

That’s how it looks:

It’s UTC+5 here. Yes, it seems that after modification I’ve got zero, but I don’t understand why and template check works as expected.

You’ve got a lot of strange unecessary conversions going on there…

{% set value = 1590950431 %}
value_template: "{{ value | int - 10800 }}" # -> value_template: "1590939631"
value_template: "{{ (value | int - 10800) | timestamp_local }}" # -> value_template: "2020-05-31 11:40:31"
value_template: "{{ (value | int - 10800)|timestamp_custom('%Y-%m-%d %H:%M:%S') }}" # -> value_template: "2020-05-31 11:40:31"

I was just a four templates for four single sensors with results values. I made them for test. The main idea is:

  1. Getting timestamp from mqtt. (successfully)
  2. Converting it to the integer. (successfully)
  3. Reducing timestamp by 10800 seconds. (successfully)
{{ value | int - 10800 }}
  1. Converting result back from numeric to the date and time. (failed)

See the last line of my code posted above:

value_template: "{{ (value | int - 10800)|timestamp_custom('%Y-%m-%d %H:%M:%S') }}" # -> value_template: "2020-05-31 11:40:31"
1 Like

Thank you very much! Works like a charm! :+1:

1 Like

If you’d like some additional information on working with time in HA/jinja here is a thread I wrote:

1 Like

Thank you again! I’ll investigate it!

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.