Skip to content

Instantly share code, notes, and snippets.

@StephenWetzel
Created June 10, 2022 06:47
Converting values which were incorrectly treated as binary, instead of BCD
select
(SUBSTRING(binary_num, 1, 4)::bit(4)::int::varchar ||
SUBSTRING(binary_num, 5, 4)::bit(4)::int::varchar ||
SUBSTRING(binary_num, 9, 4)::bit(4)::int::varchar ||
SUBSTRING(binary_num, 13, 4)::bit(4)::int::varchar ||
SUBSTRING(binary_num, 17, 4)::bit(4)::int::varchar)::int * 10 as new_state
, state_id, * from (
select right((state::int / 10)::bit(32)::varchar, 20) as binary_num, *
from states
where entity_id = 'sensor.water_meter'
and state::int > 1000000
and state not in ('unknown', 'unavailable')
) q1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment